package com.codewsq;

import org.junit.Test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketTest1 {

    @Test
    public void testClient() {
        Socket socket = null;
        OutputStream os = null;
        try {
            socket = new Socket("127.0.0.1", 8090);
            os = socket.getOutputStream();
            os.write("我是一个客户端妹子，哈哈哈哈哈哈哈呼呼呼呼呼呼呼嘿嘿嘿hi".getBytes());
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    @Test
    public void testServer() {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            serverSocket = new ServerSocket(8090);
            socket = serverSocket.accept();
            is = socket.getInputStream();
            // 这种方式,如果传输数据长度大于byte数组，会导致乱码
            /*int len;
            byte[] buffer = new byte[5];
            while ((len = is.read(buffer)) != -1) {
                String str = new String(buffer, 0, len);
                System.out.print(str);
            }*/

            // 方式一：使用转换流-- 将字节输入流转换为字符输入流然后打印出来，可避免字符乱码,记得一定要关闭isr流，这里就不关闭了
            /*InputStreamReader isr = new InputStreamReader(is);
            int len;
            char[] cbuffer = new char[5];
            while ((len = isr.read(cbuffer)) != -1) {
                String str = new String(cbuffer, 0, len);
                System.out.print(str);
            }*/

            // 方式二： 写入 字节数组流，数组流会将所有的字节缓存起来，然后转成
            baos = new ByteArrayOutputStream();
            int len;
            byte[] buffer = new byte[5];
            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            System.out.println(baos.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }

    }


}
